<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var obj = {
      name: "why",
      age: 18,
      friend: {
        name: "kobe"
      },
      toJSON: function () {
        return "123"
      }
    }

    // replacer参数
    var objJSONString = JSON.stringify(obj, function (key, value) {
      if (key === "name") {
        return 'coderwhy'
      }
      return value
    })
    console.log(objJSONString)

    // space参数
    var objJSONString1 = JSON.stringify(obj, null, 4)
    console.log(objJSONString1)

    // 直接使用toJSON

  </script>
</body>

</html>